/**
 * 
 */
package rer.persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import rer.model.Plano;
import rer.util.ConexaoJDBC_mysql;

/**
 * @author rickserrat
 *
 */
public class PlanoDAO implements IPlanoDAO {

	private Statement statement;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;
	private String query;
	private Plano plano;
	private List<Plano> planos;
	private Connection conexao = ConexaoJDBC_mysql.getConnection(); 
	
	
	/* (non-Javadoc)
	 * @see rer.persistencia.IPlanoDAO#cadastrar(rer.model.Plano)
	 */
	@Override
	public Boolean cadastrar(Plano plano) {
		// TODO Auto-generated method stub
		
		try{
			
			query = "INSERT INTO db_ceirr.planos(`descPlan`, `valorPlan`, `dataPlan`, `statusPlan`) "
					+ "VALUES (?,?,?,'A')";
			
			preparedStatement = conexao.prepareStatement(query);
			preparedStatement.setString(1, plano.getDescPlan());
			preparedStatement.setDouble(2, plano.getValorPlan());
			preparedStatement.setDate(3,new Date(plano.getDataPlan().getTime()));
			
			preparedStatement.execute();
			
			return true;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}finally {
			
			try{
				preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
		
		
		
	}

	@Override
	public Boolean editar(Plano plano) {
		try{
			
			query = "UPDATE db_ceirr.planos SET descPlan=?, valorPlan=?, dataPlan=? WHERE idPlan = ?";
			
			preparedStatement = conexao.prepareStatement(query);
			preparedStatement.setString(1, plano.getDescPlan());
			preparedStatement.setDouble(2, plano.getValorPlan());
			preparedStatement.setDate(3,new Date(plano.getDataPlan().getTime()));
			preparedStatement.setInt(4, plano.getIdPlan());
			
			preparedStatement.execute();
			
			return true;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}finally {
			
			try{
				preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return false;
	}

	
	@Override
	public Boolean deletar(int id) {
		
		try{
			
			query = "UPDATE `planos` SET statusPlan = 'I' WHERE idPlan = ?";
			
			preparedStatement = conexao.prepareStatement(query);
			preparedStatement.setInt(1, id);
			
			preparedStatement.executeUpdate();
			
			System.out.println("\nTarefa apagada com sucesso");
			
			return true;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
			
			return false;
		}

	}

	
	@Override
	public List<Plano> listar() {
		
		planos = null;
		
		try{
			
			query = "SELECT * FROM db_ceirr.planos WHERE statusPlan ='A'";
			statement = conexao.createStatement();
			resultSet = statement.executeQuery(query);
			
			planos = new ArrayList<Plano>();
			
			while (resultSet.next()){
				
				plano = new Plano();
				plano.setIdPlan(resultSet.getInt("idPlan"));
				plano.setDescPlan(resultSet.getString("descPlan"));
				plano.setValorPlan(resultSet.getDouble("valorPlan"));
				plano.setDataPlan(resultSet.getDate("dataPlan"));
				
				planos.add(plano);
				
			}
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}
		
		try {
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("\n Erro ao fechar o objeto preparedStatement.");
		}
		System.out.println("\nSucesso pesquisa");
		return planos;
	}

	/* (non-Javadoc)
	 * @see rer.persistencia.IPlanoDAO#pesquisarPlano(int)
	 */
	@Override
	public Plano pesquisarPlano(int id) {
		// TODO Auto-generated method stub
		plano = null;
		
		try{
			
			query = "SELECT * FROM db_ceirr.planos WHERE idPlan = ?";
			preparedStatement = conexao.prepareStatement(query);
			
			preparedStatement.setInt(1, id);
			
			resultSet = preparedStatement.executeQuery();
			
			if (resultSet.next()){
				
				plano = new Plano();
				plano.setIdPlan(resultSet.getInt("idPlan"));
				plano.setDescPlan(resultSet.getString("descPlan"));
				plano.setValorPlan(resultSet.getDouble("valorPlan"));
				plano.setDataPlan(resultSet.getDate("dataPlan"));
				plano.setDataPlan(resultSet.getDate("statusPlan"));
								
			}
			
			System.out.println("Pesquisa  feita plano "+ plano.getIdPlan());
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());

		} 
		
		return plano;
	}
	
	@Override
	public List<Plano> listarTodos() {
		// TODO Auto-generated method stub
		planos = null;
		
		try{
			
			query = "SELECT * FROM db_ceirr.planos";
			statement = conexao.createStatement();
			resultSet = statement.executeQuery(query);
			
			planos = new ArrayList<Plano>();
			
			while (resultSet.next()){
				
				plano = new Plano();
				plano.setIdPlan(resultSet.getInt("idPlan"));
				plano.setDescPlan(resultSet.getString("descPlan"));
				plano.setValorPlan(resultSet.getDouble("valorPlan"));
				plano.setDataPlan(resultSet.getDate("dataPlan"));
				
				planos.add(plano);
				
			}
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}
		
		try {
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("\n Erro ao fechar o objeto preparedStatement.");
		}
		System.out.println("\nSucesso pesquisa");
		return planos;
	}
		
	

}
